home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1343 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.2 KB

  1. Path: sable.ox.ac.uk!mert0053
  2. From: mert0053@sable.ox.ac.uk (Michael Brewer)
  3. Newsgroups: comp.lang.c++
  4. Subject: Deleting arrays of garbage collectables
  5. Date: 10 Jan 1996 15:35:11 GMT
  6. Organization: Oxford University, England
  7. Message-ID: <4d0mbf$6on@news.ox.ac.uk>
  8. NNTP-Posting-Host: sable.ox.ac.uk
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Probably a bad subject line, but here is what I mean:
  12.  
  13. I am using a bunch of classes that can be shared by the mechanism:
  14.  
  15. class Allo; // defined elsewhere
  16.  
  17. Allo *hi = new Allo;
  18. hi->ref(); // Using this object
  19. //.....  code
  20. hi->unref(); // finished with it. If it hasn't been ref()'ed
  21.              // elsewhere, it will be deleted
  22.  
  23.  
  24. Now what if I want an array of these? There is no satisfactory default
  25. constructor, so I need an array of pointers:
  26.  
  27. Allo **arr = new Allo * [10];
  28. for(int i = 0; i < 10; i++)
  29. {
  30.     arr[i] = new Allo(3.221);
  31.     arr[i]->ref();
  32. }
  33. // ......... code
  34. // ....
  35. // finished, unref():
  36. for(int i = 0; i < 10; i++)
  37.     arr[i]->unref();
  38.  
  39.  
  40. OK, so now the arr[i] memory has been dealt with, but what about the
  41. array of pointers arr?? There is no way of telling whether they still
  42. point to something, since some other object may still be sharing the
  43. data.
  44.  
  45. Is there a way to free up that memory?
  46.  
  47. Thanks.
  48.  
  49. Mike
  50.